package com.ama.controller;

import com.ama.dao.ApplyDao;
import com.ama.dao.ContractDao;
import com.ama.dao.CostDao;
import com.ama.dao.StaffDao;
import com.ama.pojo.*;
import com.ama.service.*;
import com.ama.utils.DateUtil;
import com.ama.utils.NumberUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class OptionController {
    @Autowired
    EstateService estateService;
    @Autowired
    BuildService buildService;
    @Autowired
    RoomService roomService;
    @Autowired
    BedService bedService;
    @Autowired
    StaffService staffService;

    @Autowired
    ApplyDao applyDao;
    @Autowired
    ContractDao contractDao;
    @Autowired
    StaffDao staffDao;
    @Autowired
    CostDao costDao;

    @RequestMapping("/agreeApply")
    public String agreeApply(HttpServletRequest request, HttpSession session){
        /**
         * 1、修改apply表中的状态 1
         * 2、修改床铺表中的状态 -1
         * 3、修改费用表中的状态 -1
         * 4、修改合同单中的状态 1 添加签订时间
         */
        Long applyId =Long.valueOf(request.getParameter("applyId"));
        Apply apply = applyDao.selectByPrimaryKey(applyId);
        apply.setApplyState(1);
        applyDao.updateByPrimaryKeySelective(apply  );

        Bed bed = bedService.selectById(apply.getBedId());
        bed.setBedState(-1);
        bedService.updateByIdSelective(bed  );


        Staff staff = staffDao.selectByPrimaryKey(apply.getStaffId());
        Cost cost = costDao.selectById(staff.getCostId());
        cost.setCostState(-1);
        cost.setCostShouldpay(NumberUtils.round((cost.getCostAmount()*3+cost.getCostTaxamount()),2));
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("cost_id",cost.getCostId());
        costDao.update(cost,updateWrapper);

        Contract contract = contractDao.selectById(staff.getContractId())  ;
        contract.setSigndate(new Date());
        contract.setContractState(1);
        UpdateWrapper wrapper1 = new UpdateWrapper();
        wrapper1.eq("contract_id",staff.getContractId());
        contractDao.update(contract,wrapper1);

        return "redirect:/success/agreeApply_suc.jsp";
    }

    @RequestMapping("/disagreeApply")
    public String disagreeApply(HttpServletRequest request, HttpSession session){
/**
 * 1、修改apply表中的状态 -1
 * 2、修改床铺表中的状态 1
 * 3、修改费用表中的状态 -3
 * 4、修改合同单中的状态 -1 添加签订时间 ，添加终止人 终止原因 终止时间
 */
        Long applyId =Long.valueOf(request.getParameter("applyId"));
        Apply apply = applyDao.selectByPrimaryKey(applyId);
        apply.setApplyState(-1);
        apply.setApplyId(applyId);
        applyDao.updateByPrimaryKeySelective(apply  );

        Bed bed = bedService.selectById(apply.getBedId());
        bed.setBedId(apply.getBedId());
        bed.setBedState(1);
        bedService.updateByIdSelective(bed  );

        Staff staff = staffDao.selectByPrimaryKey(apply.getStaffId());
        Cost cost = costDao.selectById(staff.getCostId());
        cost.setCostState(-3);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("cost_id",cost.getCostId());
        costDao.update(cost,wrapper);


        Contract contract = contractDao.selectById(staff.getContractId());
        contract.setSigndate(new Date());
        contract.setTerminalDate(new Date());
        contract.setTerminalReason("物业管理员拒绝你申请宿舍");
        Property property = (Property)session.getAttribute("user");

        contract.setTerminalPerson(property.getPropertyName());
        contract.setContractState(-1);
        UpdateWrapper wrapper1 = new UpdateWrapper();
        wrapper1.eq("contract_id",staff.getContractId());
        contractDao.update(contract,wrapper1);

        staff.setBedId(null);
        staff.setRoomId(null);
        staff.setBuildId(null);
        staff.setEstateId(null);
        staff.setApplyId(null);
        staff.setCostId(null);
        staff.setContractId(null);
        //更新员工信息
        //更新员工信息
        UpdateWrapper<Staff> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("staff_id",staff.getStaffId());
        staffDao.update(staff,updateWrapper);
        return "redirect:/success/disagreeApply_suc.jsp";
    }
}
